package com.flyan.game.motafx.ui;

import com.almasb.fxgl.core.util.LazyValue;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.flyan.game.motafx.component.BackpackComponent;
import com.flyan.game.motafx.component.EquipSlotsComponent;
import com.flyan.game.motafx.config.GameConfig;
import com.flyan.game.motafx.core.backpack.Backpack;
import com.flyan.game.motafx.core.enums.EntityTypeEnum;
import com.flyan.game.motafx.core.equip.Equip;
import com.flyan.game.motafx.core.equip.EquipSlots;
import com.flyan.game.motafx.core.util.animate.StillAnimateTable;
import javafx.geometry.Pos;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.FontSmoothingType;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * 右侧信息栏
 *
 * @author flyan
 * @version 1.0
 * @email flyan_by_chenu@sina.com
 * @since 2025/2/6
 */
public class RightInfoPane extends VBox {

    private static final int MAX_KEY_AMOUNT = 6 * 4;

    private final Text attackEquipText;
    private final Text defenseEquipText;
    private final AnimatedTexture attackEquipTexture;
    private final AnimatedTexture defenseEquipTexture;
    private final TilePane keyBox = new TilePane();

    /**
     * 物品动画
     */
    private LazyValue<StillAnimateTable> itemAnimateTable = new LazyValue<>(() -> new StillAnimateTable(
            FXGL.image("materials/items.png"), GameConfig.GRID_SIZE, GameConfig.GRID_SIZE));

    /**
     * 背包
     */
    @Setter
    private BackpackComponent backpackComponent;

    /**
     * 装备槽
     */
    @Setter
    private EquipSlotsComponent equipSlotsComponent;


    public RightInfoPane() {
        setPrefSize(GameConfig.RIGHT_INFO_PANE_WIDTH, GameConfig.RIGHT_INFO_PANE_HEIGHT);
        setTranslateX(GameConfig.RIGHT_INFO_PANE_BEGIN_COORDINATE.getX());
        setTranslateY(GameConfig.RIGHT_INFO_PANE_BEGIN_COORDINATE.getY());
        setStyle("-fx-border-color: red");

        /* 装备栏 */
        TilePane attackEquipBox = new TilePane(); // 武器
        attackEquipBox.setStyle("-fx-border-color: green");
        attackEquipBox.setTranslateX(GameConfig.RIGHT_INFO_PANE_ATTACK_EQUIP_TEXT_BEGIN_COORDINATE.getX());
        attackEquipBox.setTranslateY(GameConfig.RIGHT_INFO_PANE_ATTACK_EQUIP_TEXT_BEGIN_COORDINATE.getY());
        attackEquipText = new Text(); // 武器描述
        attackEquipText.setFontSmoothingType(FontSmoothingType.LCD);
        attackEquipText.setFill(Color.WHITE);
        attackEquipText.setStrokeWidth((double) GameConfig.RIGHT_INFO_PANE_WIDTH / 2 - 12);
        attackEquipText.setWrappingWidth((double) GameConfig.RIGHT_INFO_PANE_WIDTH / 2 - 12);
        attackEquipText.setTextAlignment(TextAlignment.CENTER);
        attackEquipText.setText("神圣剑");
        attackEquipBox.getChildren().add(attackEquipText);
        attackEquipTexture = new AnimatedTexture(itemAnimateTable.get() // 武器图
                .get(EntityTypeEnum.ITEM_DIVINE_SWORD.getType()));
        attackEquipTexture.setVisible(false);
        attackEquipTexture.loop();
        attackEquipBox.getChildren().add(attackEquipTexture);
        getChildren().add(attackEquipBox);

        TilePane defenseEquipBox = new TilePane(); // 防具
        defenseEquipBox.setStyle("-fx-border-color: green");
        defenseEquipBox.setTranslateX(GameConfig.RIGHT_INFO_PANE_ATTACK_EQUIP_TEXT_BEGIN_COORDINATE.getX());
        defenseEquipBox.setTranslateY(GameConfig.RIGHT_INFO_PANE_ATTACK_EQUIP_TEXT_BEGIN_COORDINATE.getY() + 12);
        defenseEquipText = new Text(); // 防具描述
        defenseEquipText.setFontSmoothingType(FontSmoothingType.LCD);
        defenseEquipText.setFill(Color.WHITE);
        defenseEquipText.setWrappingWidth((double) GameConfig.RIGHT_INFO_PANE_WIDTH / 2 - 12);
        defenseEquipText.setTextAlignment(TextAlignment.CENTER);
        defenseEquipText.setText("神圣盾");
        defenseEquipBox.getChildren().add(defenseEquipText);
        defenseEquipTexture = new AnimatedTexture(itemAnimateTable.get() // 防具图片
                .get(EntityTypeEnum.ITEM_DIVINE_SHIELD.getType()));
        defenseEquipTexture.setVisible(false);
        defenseEquipTexture.loop();
        defenseEquipBox.getChildren().add(defenseEquipTexture);
        getChildren().add(defenseEquipBox);

        /* 钥匙盒 */
        keyBox.setTranslateX(GameConfig.RIGHT_INFO_PANE_ATTACK_EQUIP_TEXT_BEGIN_COORDINATE.getX());
        keyBox.setTranslateY(GameConfig.RIGHT_INFO_PANE_ATTACK_EQUIP_TEXT_BEGIN_COORDINATE.getY() + 12 + 16);
        keyBox.setTileAlignment(Pos.CENTER);
        keyBox.setMaxWidth(((double) GameConfig.GRID_SIZE / 2) * 6 + 6);
        keyBox.setPrefHeight(((double) GameConfig.GRID_SIZE / 2) * 4);
        keyBox.setStyle("-fx-border-color: red");
        getChildren().add(keyBox);
    }

    public void update() {
        /* 装备 */
        EquipSlots equipSlots = equipSlotsComponent.getEquipSlots();
        Equip attackEquip = equipSlots.getAttackEquip();
        if (attackEquip != null) {
            attackEquipText.setText(attackEquip.getName());
            EntityTypeEnum equipType = (EntityTypeEnum) attackEquip.getEntity().getType();
            attackEquipTexture.setVisible(true);
            attackEquipTexture.loopAnimationChannel(itemAnimateTable.get().get(equipType.getType()));
        } else {
            attackEquipText.setText("无武器");
        }
        Equip defenseEquip = equipSlots.getDefenseEquip();
        if (defenseEquip != null) {
            defenseEquipText.setText(defenseEquip.getName());
            EntityTypeEnum equipType = (EntityTypeEnum) defenseEquip.getEntity().getType();
            defenseEquipTexture.setVisible(true);
            defenseEquipTexture.loopAnimationChannel(itemAnimateTable.get().get(equipType.getType()));
        } else {
            defenseEquipText.setText("无防具");
        }

        /* 钥匙盒 */
        Backpack backpack = backpackComponent.getBackpack();
        int keyAmount = backpack.itemAmount(EntityTypeEnum.ITEM_KEY);
        int blueKeyAmount = backpack.itemAmount(EntityTypeEnum.ITEM_BLUE_KEY);
        int redKeyAmount = backpack.itemAmount(EntityTypeEnum.ITEM_RED_KEY);
        List<EntityTypeEnum> drawKeys = new ArrayList<>(MAX_KEY_AMOUNT);
        for (int i = 0; i < redKeyAmount && drawKeys.size() < MAX_KEY_AMOUNT; i++) {
            drawKeys.add(EntityTypeEnum.ITEM_RED_KEY);
        }
        for (int i = 0; i < blueKeyAmount && drawKeys.size() < MAX_KEY_AMOUNT; i++) {
            drawKeys.add(EntityTypeEnum.ITEM_BLUE_KEY);
        }
        for (int i = 0; i < keyAmount && drawKeys.size() < MAX_KEY_AMOUNT; i++) {
            drawKeys.add(EntityTypeEnum.ITEM_KEY);
        }
        keyBox.getChildren().clear();
        for (EntityTypeEnum drawKey : drawKeys) {
            switch (drawKey) {
                case ITEM_KEY -> keyBox.getChildren().add(FXGL.texture("materials/yellow_key.png",
                        (double) GameConfig.GRID_SIZE / 2,  (double) GameConfig.GRID_SIZE / 2));
                case ITEM_BLUE_KEY -> keyBox.getChildren().add(FXGL.texture("materials/blue_key.png",
                        (double) GameConfig.GRID_SIZE / 2,  (double) GameConfig.GRID_SIZE / 2));
                case ITEM_RED_KEY -> keyBox.getChildren().add(FXGL.texture("materials/red_key.png",
                        (double) GameConfig.GRID_SIZE / 2,  (double) GameConfig.GRID_SIZE / 2));
            }
        }
    }

}
